package hlhj.fhp.supreme.fragment

import android.app.Activity
import android.content.pm.ActivityInfo
import android.content.res.Configuration
import android.net.Uri
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import cn.jzvd.JZVideoPlayer
import cn.sharesdk.framework.Platform
import cn.sharesdk.framework.PlatformActionListener
import cn.sharesdk.onekeyshare.OnekeyShare
import hlhj.fhp.supreme.R
import com.bumptech.glide.Glide
import com.gyf.barlibrary.ImmersionBar
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import fhp.hlhj.giantfold.utils.LogUtil
import fhp.hlhj.giantfold.utils.ToastUtils
import hlhj.fhp.supreme.activitys.OptionDetailAty
import hlhj.fhp.supreme.javabean.BaseBean
import hlhj.fhp.supreme.javabean.OptionDetailBean
import hlhj.fhp.supreme.network.Urls
import hlhj.fhp.supreme.utils.JsonCallBack
import hlhj.fhp.supreme.utils.MyUtils
import kotlinx.android.synthetic.main.option_video.view.*


/**
 * Created by Administrator on 2018\3\12 0012.
 */
class OptionDetailVideoFgm :Fragment() {
    private lateinit var rootView:View
    var url = "http://9890.vod.myqcloud.com/9890_9c1fa3e2aea011e59fc841df10c92278.f20.mp4"
    var imgUrl = "http://p.qpic.cn/videoyun/0/2449_43b6f696980311e59ed467f22794e792_1/640"
    companion object {
        fun newInstence(id:Int): OptionDetailVideoFgm {
            val vediotFgm = OptionDetailVideoFgm()
            val bundle = Bundle()
            bundle.putInt("id",id)
            vediotFgm.arguments=bundle
            return vediotFgm
        }
    }
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
         rootView = inflater?.inflate(R.layout.option_video, null)

        val id = arguments.getInt("id")
        OkGo.post<OptionDetailBean>(Urls.BaseUrl+ Urls.OPTIN_DETAIL)
                .params("id",id.toString())
                .execute(object : JsonCallBack<OptionDetailBean>(activity){
                    override fun onSuccess(response: Response<OptionDetailBean>?) {
                        val bean = response?.body()
                        if (bean?.code==1){
                            rootView.tvInfo.text=bean?.data.desc
                            rootView.tvTittle.text=bean?.data.title
                            url=bean?.data.src
                            rootView.videoPlayer.setUp(url,JZVideoPlayer.SCREEN_WINDOW_NORMAL,bean?.data.title)
//                            rootView.videoPlayer.setUp("http://cdn.376600500.com/20180518114754/FpYT57por7t_j46BkBMBbWxkYno0",JZVideoPlayer.SCREEN_WINDOW_NORMAL,bean?.data.title)
                            JZVideoPlayer.FULLSCREEN_ORIENTATION = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
                            JZVideoPlayer.NORMAL_ORIENTATION = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
                        }else{
                            MyUtils.toast(bean?.msg)
                        }
                    }
                })
        initListener()
        return rootView
    }

    private fun initListener() {
       rootView.btShare.setOnClickListener {
            val listener=object : PlatformActionListener {
                override fun onComplete(p0: Platform?, p1: Int, p2: HashMap<String, Any>?) {
                    OkGo.post<BaseBean>(Urls.BaseUrl+Urls.SHARE)
                            .execute(object :JsonCallBack<BaseBean>(activity){
                                override fun onSuccess(response: Response<BaseBean>?) {
                                    val bean = response?.body()
                                    if (bean?.code==1){
                                        ToastUtils.toast(activity,"分享成功")
                                    }else{
                                        ToastUtils.toast(activity,bean?.msg!!)
                                    }
                                }
                            })
                }

                override fun onCancel(p0: Platform?, p1: Int) {
                    ToastUtils.toast(context,"分享已取消")
                }

                override fun onError(p0: Platform?, p1: Int, p2: Throwable?) {
                    ToastUtils.toast(context,"分享失败")
                }
            }
           val oks = OnekeyShare()
           oks.callback=listener
           oks.setTitle("志尚课堂")
           // titleUrl是标题的网络链接，仅在Linked-in,QQ和QQ空间使用
           oks.setTitleUrl("https://www.baidu.com/")
           // text是分享文本，所有平台都需要这个字段
           oks.text = rootView.tvTittle.text.toString()
           //分享网络图片，新浪微博分享网络图片需要通过审核后申请高级写入接口，否则请注释掉测试新浪微博
           oks.setImageUrl("http://zhishang.hlhjapp.com/static/images/logo.png")
           // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
           //oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
           // url仅在微信（包括好友和朋友圈）中使用
           oks.setUrl("https://www.baidu.com/")
           // comment是我对这条分享的评论，仅在人人网和QQ空间使用
           oks.show(context)
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        JZVideoPlayer.releaseAllVideos()
        val optionDetailAty = activity as OptionDetailAty
        optionDetailAty.supportActionBar?.hide()
    }
    override fun onPause() {
        super.onPause()
        JZVideoPlayer.releaseAllVideos()
        val optionDetailAty = activity as OptionDetailAty
        optionDetailAty.supportActionBar?.hide()
        /*ImmersionBar.with(this)
                .statusBarDarkFont(false)
                .init()*/
        //Change these two variables back
        JZVideoPlayer.FULLSCREEN_ORIENTATION = ActivityInfo.SCREEN_ORIENTATION_SENSOR
        JZVideoPlayer.NORMAL_ORIENTATION = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
    }
}